package zhr.chapter10;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.Iterator;
import java.util.List;

public class Example10_6 {
    public static void main(String[] args) {
        // 获取项目的根目录路径
        String projectPath = System.getProperty("user.dir");
        String filePath = projectPath + File.separator + "src" + File.separator + "main" + File.separator + "java" + File.separator + "zhr" + File.separator + "chapter10"  + File.separator + "appconfig.xml";
        SAXReader reader=new SAXReader();
        String path="C:\\Users\\SD\\IdeaProjects\\javaExample\\src\\main\\java\\zhr\\chapter10\\appconfig.xml";
        System.out.println(path);
        System.out.println(filePath);
        File file=new File(filePath);
        if(file.exists()){
            try{
                Document document=reader.read(file);
                Element root=document.getRootElement();
                Element appSettingsEle=root.element("appSettings");
                List connectionStringList=appSettingsEle.elements("connectionString");
                Iterator it =connectionStringList.iterator();
                while (it.hasNext()){
                    Element connectionStringEle=(Element) it.next();
                    System.out.print(connectionStringEle.attribute("url").getText()+" ");
                    System.out.print(connectionStringEle.attribute("uid").getText()+" ");
                    System.out.println(connectionStringEle.attribute("pwd").getText()+" ");
                }
            }catch (DocumentException e){
                System.err.println(e.getMessage());
            }
        }
    }
}
